﻿namespace hedefgrup.reports.renault.authentication
{
    using hedefgrup.reports.renault.database;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class user
    {
        private string firstName;
        private long id;
        private bool isActive;
        private bool isAdministrator;
        private bool isDeleted;
        private string lastName;
        private string newPassword;
        private string userName;

        private user(long id, string userName, string firstName, string lastName, bool isActive, bool isAdministrator)
        {
            this.id = id;
            this.userName = userName;
            this.firstName = firstName;
            this.lastName = lastName;
            this.isActive = isActive;
            this.isAdministrator = isAdministrator;
        }

        public static bool exists(string userName)
        {
            return (db.query("SELECT [id] FROM [user] WHERE [user_name] = @p_0", new object[] { userName }).Rows.Count > 0);
        }

        public static user[] getAllUsers()
        {
            List<user> list = new List<user>();
            DataTable table = db.query("SELECT * FROM [user] ORDER BY [id] ASC", null);
            foreach (DataRow row in table.Rows)
            {
                list.Add(getUserFromDataRow(row));
            }
            return list.ToArray();
        }

        public string getFirstName()
        {
            this.throwIfDeleted();
            return this.firstName;
        }

        public long getID()
        {
            this.throwIfDeleted();
            return this.id;
        }

        public bool getIsActive()
        {
            this.throwIfDeleted();
            return this.isActive;
        }

        public bool getIsAdministrator()
        {
            this.throwIfDeleted();
            return this.isAdministrator;
        }

        public string getLastName()
        {
            this.throwIfDeleted();
            return this.lastName;
        }

        public static user getUserByCredentials(string userName, string password)
        {
            return getUserFromDataTable(db.query("SELECT * FROM [user] WHERE [user_name] = @p_0 AND pwdCompare(@p_1, password) = 1", new object[] { userName, password }));
        }

        public static user getUserById(long id)
        {
            return getUserFromDataTable(db.query("SELECT * FROM [user] WHERE [id] = @p_0", new object[] { id }));
        }

        private static user getUserFromDataRow(DataRow userDataRow)
        {
            return new user(Convert.ToInt64(userDataRow["id"]), Convert.ToString(userDataRow["user_name"]).Trim(), Convert.ToString(userDataRow["first_name"]).Trim(), Convert.ToString(userDataRow["last_name"]).Trim(), Convert.ToBoolean(userDataRow["active"]), Convert.ToBoolean(userDataRow["is_administrative_user"]));
        }

        private static user getUserFromDataTable(DataTable userData)
        {
            if (userData.Rows.Count == 0)
            {
                return null;
            }
            return getUserFromDataRow(userData.Rows[0]);
        }

        public string getUserName()
        {
            this.throwIfDeleted();
            return this.userName;
        }

        public bool Insert()
        {
            this.throwIfDeleted();
            if (this.id > 0L)
            {
                return false;
            }
            DataTable table = db.query("INSERT INTO [user] (user_name, password, first_name, last_name, active, is_administrative_user) VALUES (@p_0, pwdencrypt(@p_1), @p_2, @p_3, @p_4, 0)\r\nSELECT @@IDENTITY", new object[] { this.userName, this.newPassword, this.firstName, this.lastName, this.isActive });
            this.id = Convert.ToInt64(table.Rows[0][0]);
            return true;
        }

        public static user New()
        {
            return new user(0L, "", "", "", false, false);
        }

        public void Remove()
        {
            this.throwIfDeleted();
            db.execute("DELETE [user] WHERE [id] = @p_0", new object[] { this.id });
            this.isDeleted = true;
        }

        public void setActivationStatus(bool isActive)
        {
            this.isActive = isActive;
        }

        public void setFirstName(string newValue)
        {
            this.firstName = newValue;
        }

        public void setLastName(string newValue)
        {
            this.lastName = newValue;
        }

        public void SetPassword(string newPassword)
        {
            this.throwIfDeleted();
            this.newPassword = newPassword;
        }

        public bool SetPassword(string oldPassword, string newPassword)
        {
            this.throwIfDeleted();
            if (this.id <= 0L)
            {
                return false;
            }
            DataTable table = db.query("SELECT * FROM [user] WHERE [id] = @p_0 AND pwdCompare(@p_1, [password]) = 1", new object[] { this.id, oldPassword });
            if (table.Rows.Count == 1)
            {
                this.newPassword = newPassword;
            }
            return (table.Rows.Count == 1);
        }

        public bool setUserName(string userName)
        {
            if (exists(userName))
            {
                return false;
            }
            this.userName = userName;
            return true;
        }

        private void throwIfDeleted()
        {
            if (this.isDeleted)
            {
                throw new InvalidOperationException("No operation can be made on a deleted instance");
            }
        }

        public bool Update()
        {
            this.throwIfDeleted();
            if (this.id <= 0L)
            {
                return false;
            }
            bool flag = !string.IsNullOrEmpty(this.newPassword);
            object[] args = new object[flag ? 6 : 5];
            args[0] = this.firstName;
            args[1] = this.lastName;
            args[2] = this.isActive;
            args[3] = this.id;
            args[4] = this.userName;
            if (flag)
            {
                args[5] = this.newPassword;
            }
            db.execute("UPDATE [user] SET [user_name] = @p_4, password = " + (flag ? "pwdencrypt(@p_5)" : "password") + ", first_name = @p_0, last_name = @p_1, active = @p_2 WHERE [id] = @p_3", args);
            this.newPassword = null;
            return true;
        }
    }
}

